package cn.zwx.distributed.svc.rpc;

import cn.zwx.base.entity.ResponseEntity;
import cn.zwx.base.exception.BizWrapException;
import cn.zwx.base.utils.ValidationUtils;
import cn.zwx.distributed.svc.rpc.facade.DubboDistributedService;
import cn.zwx.distributed.svc.service.UidGeneratorService;
import org.apache.dubbo.apidocs.annotations.ApiDoc;
import org.apache.dubbo.apidocs.annotations.ApiModule;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author zhangwenxue
 */
@Service
@ApiModule(value = "生成分布式ID相关接口", apiInterface = DubboDistributedService.class)
@DubboService(version = "v1.0.0",interfaceClass = DubboDistributedService.class,timeout = 8000)
public class DubboDistributedServiceImpl implements DubboDistributedService {

    @Resource(name="cachedUidGeneratorServiceImpl")
    private UidGeneratorService cachedUidGeneratorService;

    @Override
    @ApiDoc(value = "获取全局分布式ID接口")
    public ResponseEntity<String> getDistributedId() throws BizWrapException {
        long distributedId = cachedUidGeneratorService.getDistributedId();
        return ResponseEntity.success(String.valueOf( distributedId));
    }

    @Override
    @ApiDoc(value = "解析全局分布式ID接口")
    public ResponseEntity<String> parsDistributedId(String distributedId) throws BizWrapException {
        ValidationUtils.getInstance().validate(distributedId);
        String parsDistributedId = cachedUidGeneratorService.parsDistributedId(Long.parseLong(distributedId));
        return ResponseEntity.success(parsDistributedId);
    }
}
